package com.kingdee.jdy.controller;

import com.alibaba.fastjson.JSONObject;
import com.kingdee.jdy.util.HttpUtil;
import java.util.HashMap;
import java.util.Random;

/**
 * 单位
 */
public class UnitController {

    public static long saveUnit(HashMap<String, Object> urlParam, HashMap<String, Object> headerParam) throws Exception
    {
        long unitId = 0;
        JSONObject resUnit = HttpUtil
            .doPost("http://api.kingdee.com/jdy/basedata/measure_unit_save", urlParam, headerParam, prepareUnitParam().toJSONString());

        if (resUnit != null) {
            JSONObject data = resUnit.getJSONObject("data");
            if (data != null) {
                String success = data.getString("success");
                if (success.equalsIgnoreCase("true")) {
                    unitId = data.getJSONArray("successPkIds").getLongValue(0);
                    System.out.println("同步单位成功！unitId=" + unitId);
                } else {
                    System.out.println("同步单位失败！");
                }
            } else {
                System.out.println("同步单位失败！");
            }
        } else {
            System.out.println("同步单位失败！");
        }
        return unitId;
    }

    public static JSONObject prepareUnitParam(){
        /*
        {
          "id":"784779802711187456",
          "precision": 4,
          "name": "测试"
      }
         */
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", null);
        jsonObject.put("precision", 4);
        jsonObject.put("name", "demo测试单位" + new Random().nextInt());
        return jsonObject;
    }



}
